package com.wangxiang.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.expr.NewArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class StrChangeMapUtil {
    /**
     * 自定义一个string转map的工具
     * @param str
     * @return
     */
    public HashMap<String,Object> map(String str){
        HashMap<String, Object> map = new HashMap<>();
        //拿到字符串
//        String str = "{图案=纯色, 货号=LAXIA1312, 商品毛重=1.0kg, 裙型=其他, 廓形=A型, 面料=针织, 袖型=常规袖, 衣门襟=套头, 腰型=自然腰, 商品编号=10043688074630, 店铺= La Chapelle曼静专卖店, 上市时间=2022年春季, 商品名称=拉夏贝尔2022春季早春新款女装韩版甜美淑女宽松慵懒风外穿针织连衣裙 白色毛衣+吊带裙 L【建议100-110斤】, 材质=涤纶(聚酯纤维)100%, 领型=V领, 裙长=长裙, 袖长=长袖, 风格=通勤}";
        //去掉两边的{},替换为,
        String str1 = str.replace("{","");
        String str2 = str1.replace("}",","); //成功两边的{}
        //以逗号为标识分割字符
        String[] split = str2.split(",");
        for (int i = 0; i < split.length; i++) {
            //=号以前为key,=号以后为value，识别所有的空格，去除
            String replace = split[i].replace(" ", "");
            String key = StringUtils.substringBefore(replace,"=");
            String value = StringUtils.substringAfter(replace,"=");
            if (key!=null && value!=null){
                map.put(key,value);
            }
        }

        return map;
    }

    /**
     * 前端会带着空的key,value，处理掉
     * @param str
     * @return
     */
    public String devStr(String str){
        StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
        HashMap<String, Object> map = strChangeMapUtil.map(str);
        HashMap<String, Object> map1 = new HashMap<>();
        if (map!=null){
            String s = map.toString();
            //去掉两边
            String replace = s.replace("{", "");
            String replace1 = replace.replace("}", "");
            String replace2 = replace1.replace("null=null", "");
            //截取 ， 号
            String[] split = replace2.split(", ");
            for (int i = 0; i < split.length; i++) {
                String s1 = StringUtils.substringBefore(split[i], "=");
                String s2 = StringUtils.substringAfter(split[i], "=");
                map1.put(s1,s2);
            }
        }


        if (map1!=null){
            String s = map1.toString();
//            System.out.println("工具返回的paramStr"+map1.toString());
            return s;
        }
        return null;
    }

    //处理color ,size , num的字符串，前端是数据样式为[color=111,size=111,num=111]|[color=222,size=222,num=222]|[color=333,size=333,num=333]|[color=444,size=444,num=444]
    public List<HashMap<String,Object>> devColorSizeNum(String str){
        HashMap<String, List<Object>> stringListHashMap = new HashMap<>();
        //colorList,sizeList,skuMap(color=?,size=?,num=?)
        ArrayList<HashMap<String,Object>> skuList = new ArrayList<>();
        ArrayList<Object> colorList = new ArrayList<>();
        ArrayList<Object> sizeList = new ArrayList<>();
        if (str!=null){
            //截断|符号,因为|是特殊符号，所以要加\\转义，不能直接使用split("|")
            String[] split = str.split("\\|");
            for (int i = 0; i < split.length; i++) {
                //去掉两边的[]
                String replace = split[i].replace("[", "");
                String replace1 = replace.replace("]", "");
                HashMap<String, Object> sku = new HashMap<>();
                //截断每一行每一个属性
                String[] split1 = replace1.split(",");
                for (int j = 0; j < split1.length; j++) {
                    String after = StringUtils.substringAfter(split1[j],"=");
                    String before = StringUtils.substringBefore(split1[j],"=");
                    //颜色
                    if (before.equals("color")){
                        if (!after.equals("null")){
                            sku.put("color",after);
                            colorList.add(after);
                        }
                    }
                    //尺寸
                    if (before.equals("size")){
                        if (!after.equals("null")){
                            sku.put("size",after);
                            sizeList.add(after);
                        }
                    }
                    //数量
                    if (before.equals("num")){
                        if (!after.equals("null")){
                            sku.put("num",after);
                        }
                    }
                }
                skuList.add(sku);
            }
            HashMap<String, Object> colorMap = new HashMap<>();
            HashMap<String, Object> sizeMap = new HashMap<>();
            if (colorList.size()!=0){
                colorMap.put("colorList",colorList.toString());
            }
            if (sizeList.size()!=0){
                sizeMap.put("sizeList",sizeList.toString());
            }
            skuList.add(colorMap);
            skuList.add(sizeMap);
        }

        return skuList;
    }


}
